package com.fudan.backend.controller;

import com.fudan.backend.exception.BaseException;
import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.Account;
import com.fudan.backend.pojo.statics.AccountStatic;
import com.fudan.backend.service.AccountService;
import com.fudan.backend.util.JwtUtil;
import com.fudan.backend.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    AccountService accountService;

    @GetMapping("/personal/info")
    public R getAccountByAccountId(String userId, @RequestHeader String token) {
        if (!JwtUtil.getUserId(token).equals(userId)) {
            return new R(RetCode.UNAUTHORIZED_REQUEST.getCode(), RetCode.UNAUTHORIZED_REQUEST.getMessage());
        }
        Account account = accountService.getAccountByUserId(userId);
        return new R(RetCode.ACCOUNT_SUCCESS.getCode(), RetCode.ACCOUNT_SUCCESS.getMessage(), "", account);
    }

    @GetMapping("/store")
    public R getAllStoreAccountByUserId(String userId, @RequestHeader String token) {
        if (!JwtUtil.getUserId(token).equals(userId)) {
            return new R(RetCode.UNAUTHORIZED_REQUEST.getCode(), RetCode.UNAUTHORIZED_REQUEST.getMessage());
        }
        List<Account> accounts = accountService.getAllStoreAccountByUserId(userId);
        return new R(RetCode.ACCOUNT_SUCCESS.getCode(), RetCode.ACCOUNT_SUCCESS.getMessage(), "", accounts);
    }

    @PostMapping("/personal/recharge")
    public R rechargeAccountByUserId(@RequestBody Map<String, Object> map) {
        String userId;
        double amount;
        try {
            userId = map.get("userId").toString();
            amount = Double.parseDouble(map.get("amount").toString());
        } catch (Exception e) {
            throw new BaseException(RetCode.INVALID_REQUEST.getCode(), RetCode.INVALID_REQUEST.getMessage());
        }
        RetCode retCode = accountService.rechargeByUserId(userId, amount);
        return new R(retCode.getCode(), retCode.getMessage());
    }

    @PostMapping("/store/recharge")
    public R rechargeAccountByStoreId(@RequestBody Map<String, Object> map) {
        String storeId;
        double amount;
        try {
            storeId = map.get("storeId").toString();
            amount = Double.parseDouble(map.get("amount").toString());
        } catch (Exception e) {
            throw new BaseException(RetCode.INVALID_REQUEST.getCode(), RetCode.INVALID_REQUEST.getMessage());
        }
        RetCode retCode = accountService.rechargeByStoreId(storeId, amount);
        return new R(retCode.getCode(), retCode.getMessage());
    }

    @GetMapping("/webshop")
    public R getWebshopAccount() {
        Account account = accountService.getAccountByAccountId(AccountStatic.WEBSHOP_ACCOUNT_ID);
        return new R(RetCode.ACCOUNT_SUCCESS, account);
    }

    @GetMapping("/store/{storeId}")
    public R getAccountByStoreId(@PathVariable("storeId") String storeId) {
        Account account = accountService.getAccountByStoreId(storeId);
        return new R(RetCode.ACCOUNT_SUCCESS, account);
    }
    @PostMapping("/webshop/recharge")
    public R webshopAccountRecharge(@RequestBody Map<String, Object> map){
        double amount;
        try {
            amount = Double.parseDouble(map.get("amount").toString());
        } catch (Exception e) {
            throw new BaseException(RetCode.INVALID_REQUEST.getCode(), RetCode.INVALID_REQUEST.getMessage());
        }
        accountService.recharge(AccountStatic.WEBSHOP_ACCOUNT_ID, amount);
        return new R(RetCode.ACCOUNT_SUCCESS);
    }
}
